﻿using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Commands;
using Samba.Presentation.Common;
using Samba.Services;
using System.Windows.Input;

namespace Samba.Modules.CreditCardModule.ExternalProcessor
{
    public class OnProcessedArgs
    {
        public ProcessType ProcessType { get; set; }
    }

    [Export]
    public class ExternalProcessorViewModel : ObservableObject
    {
        public delegate void OnProcessed(object sender, OnProcessedArgs args);
        public event OnProcessed Processed;

        private void InvokeProcessed(OnProcessedArgs args)
        {
            OnProcessed handler = Processed;
            if (handler != null) handler(this, args);
        }

        [ImportingConstructor]
        public ExternalProcessorViewModel()
        {
            ForceCommand = new DelegateCommand(OnForce);
            PreAuthCommand = new DelegateCommand(OnPreAuth, CanPreAuthExecute);
            CancelCommand = new DelegateCommand(OnCancel);
            ManualSwipeToggleCommand = new DelegateCommand(OnManualSwipeToggle);
            TypeValueCommand = new DelegateCommand<string>(OnTypeValueExecuted);
        }

        private bool CanPreAuthExecute()
        {
            return CanPreAuth;
        }

        private void OnPreAuth()
        {
            var args = new OnProcessedArgs { ProcessType = ProcessType.PreAuth };
            InvokeProcessed(args);
        }
        private void OnTypeValueExecuted(string text)
        {

            var target = Keyboard.FocusedElement;
            var routedEvent = TextCompositionManager.TextInputEvent;
            if (text.Equals("C"))
            {
                KeyEventArgs e = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource,
                                                  0, Key.Back);
                e.RoutedEvent = Keyboard.KeyDownEvent;
                target.RaiseEvent(e);
            }
            else
            {
                target.RaiseEvent(
                    new TextCompositionEventArgs(
                    InputManager.Current.PrimaryKeyboardDevice,
                        new TextComposition(InputManager.Current, target, text)) { RoutedEvent = routedEvent }
                );
            }
        }
        



        
        private void OnCancel()
        {
            InvokeProcessed(new OnProcessedArgs { ProcessType = ProcessType.Cancel });
        }

        private void OnForce()
        {
            InvokeProcessed(new OnProcessedArgs { ProcessType = ProcessType.Force });
        }

        private void OnManualSwipeToggle()
        {
            InvokeProcessed(new OnProcessedArgs { ProcessType = ProcessType.Manual });
        }

        private void OnSwipe()
        {
            InvokeProcessed(new OnProcessedArgs { ProcessType = ProcessType.Swipe });
        }

        public DelegateCommand PreAuthCommand { get; set; }
        public DelegateCommand ForceCommand { get; set; }
        public DelegateCommand CancelCommand { get; set; }
        public DelegateCommand ManualSwipeToggleCommand { get; set; }
        public DelegateCommand<string> TypeValueCommand { get; set; }
        public DelegateCommand CardSwipeCommand { get; set; }

        public decimal TenderedAmount { get; set; }
        public decimal Gratuity { get; set; }
        public string AuthCode { get; set; }
        public string CardholderName { get; set; }
        public bool CanPreAuth { get; set; }
    }

}
